//
//  ZCYuLanShiJuanHelper.m
//  TingShuoTeacher
//
//  Created by TJXF-JY-ZHANGCHENG on 2018/5/3.
//  Copyright © 2018年 ZhangCheng. All rights reserved.
//

#import "ZCYuLanShiJuanHelper.h"
#import "ZCPaperManager.h"
#import "ZCFileManager.h"
#import "ShiJuanYuLanViewController.h"
@interface ZCYuLanShiJuanHelper()
@end

@implementation ZCYuLanShiJuanHelper

+(ZCYuLanShiJuanHelper *)sharedInstance{
    static ZCYuLanShiJuanHelper *zcTool;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        zcTool = [[ZCYuLanShiJuanHelper alloc]init];
    });
    return zcTool;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

-(ShiJuanYuLanViewController*)vcForPaperId:(NSString *)paperId{
    ShiJuanYuLanViewController *result = nil;
    NSData *paperData = [[NSData alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/paper.json",[[ZCFileManager sharedInstance]directPathForFileOfId:paperId]]];
    NSError *error;
    NSDictionary* paperDic = [NSJSONSerialization JSONObjectWithData:paperData options:NSJSONReadingMutableContainers error:&error];
    if (error) {
        [SVProgressHUD showErrorWithStatus:@"不是有效的试卷格式"];
    }else{
        NSLog(@"%@",paperDic);
        result = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"SJYL"];
        result.paperDic = paperDic;
        result.paperId = paperId;
    }
    return result;
}

-(void)pushYuLanShiJuanViewControllerForFileKey:(NSString *)fileKey andPaperId:(NSString *)paperId andPusher:(UINavigationController *)nav{
    [self pushYuLanShiJuanViewControllerForFileKey:fileKey andPaperId:paperId andPusher:nav isTBLX:NO unitName:nil lessonName:nil ceCiName:nil];
}

-(void)pushYuLanShiJuanViewControllerForFileKey:(NSString *)fileKey andPaperId:(NSString *)paperId andPusher:(UINavigationController *)nav isTBLX:(BOOL)isTBLX unitName:(NSString *)unitNameString lessonName:(NSString*)lessonNameString ceCiName:(NSString *)ceCiName{
    if ([[ZCFileManager sharedInstance]existFileOfId:paperId]) {
        ShiJuanYuLanViewController *vc = [self vcForPaperId:paperId];
        if (vc!=nil) {
            vc.isTBLX = isTBLX;
            vc.unitNameString = unitNameString;
            vc.lessonNameString = lessonNameString;
            vc.ceCiNameString = ceCiName;
            [nav pushViewController:vc animated:YES];
        }
    }else{
        NSLog(@"开始下载");
        NSString *urlString = [NSString stringWithFormat:@"%@/file/get-download-url",JSXF_HOST];
        NSDictionary *params = @{
                                 @"fileKey":fileKey
                                 };
        
        [SVProgressHUD show];
        
        [ZCWebServiceHelper requestWithMethod:POST andUrlStr:urlString andParameters:params andFinished:^(id response, NSURLSessionDataTask *dataTask) {
            NSDictionary *dic2 = response;
            NSString *path = dic2[@"data"];
            [[ZCFileManager sharedInstance]downloadAndUnzipFileOfId:paperId Andpath:path completionBlock:^{
                NSLog(@"下载成功a");
                ShiJuanYuLanViewController *vc = [self vcForPaperId:paperId];
                if (vc!=nil) {
                    vc.isTBLX = isTBLX;
                    vc.unitNameString = unitNameString;
                    vc.lessonNameString = lessonNameString;
                    vc.ceCiNameString = ceCiName;
                    [nav pushViewController:vc animated:YES];
                }
                
            } failedBlock:^{
                NSLog(@"下载失败");
            }];
        }];
    }
}

@end
